<?php
require_once 'util/perm.php';
require_once 'db/db.php';

function deletePost( $threadNum, $postNum ) {
  if( !hasPerm('delete.post') ) 
    return -1;

  try {
    // XXX deleteing all posts will leave hanging thread
   require 'db/get/post.php';  
   if( postCount($threadNum) < 2 ) {
      // XXX Modify to link post MIN(post.rank) to thread
      die("Please Delete Thread Instead");
    }
    $db = openDbConn();
    $db->beginTransaction();
    $db->exec("DELETE FROM post WHERE post.tid = $threadNum AND post.rank = $postNum;");
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

function newThread( $uid, $forumNum, $title, $datum, &$threadNum = true, $mode = "none") {
  if( !hasPerm('new.thread') ) 
    return -1;

  try {
    $db = openDbConn();

    $title = $db->quote($title);
    $datum = $db->quote($datum);
    $mode = $db->quote($mode);
    $time = $db->quote(time());
    $uid = $db->quote($uid);

    $db->beginTransaction();
      $db->exec("INSERT INTO thread (fid, mode) 
                                    VALUES ( ${forumNum}, $mode );
                                  INSERT INTO post (tid, uid, rank, title, datum, date, parent) 
                                    VALUES( (SELECT MAX(id) FROM thread), 
                                            $uid,
                                            1+ COALESCE( (SELECT MAX(post.rank) FROM post
                                                             WHERE post.tid = (SELECT MAX(id) FROM thread ) ),
                                                      0 ), 
                                            $title, 
                                            $datum,
                                            $time,
                                            0);");
    if( $threadNum == false ) {
      // Another more transporatble query is "WHERE time=${time} AND datum=${datum} AND uid=${uid}"
      // Not sure if this will always return the correct value, but it should be pretty damn close.
      $post = $db->query('SELECT tid, rank FROM post WHERE post.id = '. $db->lastInsertId() .';' )->fetch(PDO::FETCH_ASSOC);
      $threadNum = $post['tid'];
      $postNum = $post['rank'];
      $post = NULL;
    }
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
function newPost(&$threadNum, $title, $datum, &$postNum = true) { 
  if( !hasPerm('new.post') ) 
    return -1;

  try {
    $db = openDbConn();

    $uid = $_SESSION['uid'];
    $title = $db->quote($title);
    $datum = $db->quote($datum);
    $time = $db->quote(time());
    $uid = $db->quote($uid);

    $db->beginTransaction();
    $db->exec("INSERT INTO post (tid, uid, rank, title, datum, date, parent) 
                                    VALUES( ${threadNum}, 
                                            $uid,
                                            1+ COALESCE( (SELECT MAX(post.rank) FROM post
                                                             WHERE tid = {$threadNum} ),
                                                      0), 
                                            $title, 
                                            $datum,
                                            $time,
                                            0);");    
    if( $postNum == false ) {
      // Another more transporatble query is "WHERE time=${time} AND datum=${datum} AND uid=${uid}"
      // Not sure if this will always return the correct value, but it should be pretty damn close.
      $post = $db->query('SELECT tid, rank FROM post WHERE post.id = '. $db->lastInsertId() .';' )->fetch(PDO::FETCH_ASSOC);
      $threadNum = $post['tid'];
      $postNum = $post['rank'];
      $post = NULL;
    }
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
function setPost( $threadNum, $postNum, $title, $datum) {

  try {
    $db = openDbConn();

    $title = $db->quote( $title );
    $datum = $db->quote( $datum );

    $postNum = $db->quote( $postNum );
    $threadNum = $db->quote( $threadNum );
    $uid = $db->quote($_SESSION['uid']);
    $time = $db->quote(time());

    $db->beginTransaction();
    if( hasPerm('edit.thread') )
      $ret = $db->exec("UPDATE post 
                                  SET title = $title, 
                                      datum = $datum,
                                      date = $time 
                                  WHERE rank = $postNum
                                    AND tid = $threadNum;");
    else 
      $ret = $db->exec("UPDATE post 
                                  SET title = $title, 
                                      datum = $datum,
                                      date = $time 
                                  WHERE rank = $postNum
                                    AND tid = $threadNum
                                    AND uid = $uid;");
    $db->commit();

    return $res;
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
?>
